package com.baidu.synthesis.config.baidu;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.UUID;

/**
 * @author liuzhiqiang
 */
@Component
public class BaiduApi {

    public static final String TAKEN_URL = "https://openapi.baidu.com/oauth/2.0/token";

    public static final String TEXT2AUDIO = "https://tsn.baidu.com/text2audio";
    @Autowired
    private RestTemplate restTemplate;

    /**
     * @return {
     * "access_token": "1.a6b7dbd428f731035f771b8d********.86400.1292922000-2346678-124328",
     * "expires_in": 86400,
     * "refresh_token": "2.385d55f8615fdfd9edb7c4b********.604800.1293440400-2346678-124328",
     * "scope": "public audio_voice_assistant_get 。。。",
     * "session_key": "ANXxSNjwQDugf8615Onqeik********CdlLxn",
     * "session_secret": "248APxvxjCZ0VEC********aK4oZExMB",
     * }
     */
    public JSONObject getAccessToken() {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Content-Type", "application/x-www-form-urlencoded");
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>(2);
        postParameters.add("grant_type", "client_credentials");
        postParameters.add("client_id", BaiduConfig.apiKey);
        postParameters.add("client_secret", BaiduConfig.secretKey);
        HttpEntity<Map> requestEntity = new HttpEntity<>(postParameters, requestHeaders);
        ResponseEntity<JSONObject> entity = restTemplate.exchange(TAKEN_URL, HttpMethod.POST, requestEntity, JSONObject.class);
        return entity.getBody();
    }

    public ResponseEntity<byte[]> onlineSynthesis(String text) {
        JSONObject jsonObject = getAccessToken();
        String token = jsonObject.getString("access_token");
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Content-type", "application/x-www-form-urlencoded");

        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>(2);
//        try {
//            postParameters.add("tex", URLEncoder.encode(URLEncoder.encode(text,"UTF-8"),"UTF-8"));
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        postParameters.add("tex", text);
        postParameters.add("tok", token);
        postParameters.add("cuid", UUID.randomUUID().toString());
        postParameters.add("ctp", 1);
        postParameters.add("lan", "zh");
        HttpEntity<Map> requestEntity = new HttpEntity<>(postParameters, requestHeaders);
        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(TEXT2AUDIO, HttpMethod.POST, requestEntity, byte[].class);
        return responseEntity;
    }


}
